package com.mzm.config;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import org.joda.time.base.BaseDateTime;

import com.thoughtworks.xstream.XStream;

/**
 * 
 * @author Daniel Orr
 * 
 */
@Entity(name = "Parameter")
@Table(name = "CONFIG")
public class Parameter implements Serializable {

	private static final long serialVersionUID = 1L;
	private static XStream xStream;

	static {
		xStream = new XStream();
		xStream.omitField(BaseDateTime.class, "iChronology");
	}
	
	@Id
	protected Integer id;

	@Column(name = "SECTION")
	private String section;

	@Column(name = "PARAM")
	private String name;

	@Column(name = "VALUE")
	private String value;

	public Parameter() {
	}

	public Parameter(String section, String name) {
		this.section = section;
		this.name = name;
	}

	public Parameter(String section, String name, String value) {
		this.section = section;
		this.name = name;
		this.value = value;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getSection() {
		return section;
	}

	public String getName() {
		return name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public void setValue(Object value) {
		this.value = xStream.toXML(value);
	}

	public boolean getBooleanValue() {
		return "true".equalsIgnoreCase(value);
	}

	public Object getObjectValue() {
		return xStream.fromXML(value);
	}

	public int getIntValue() {
		return Integer.parseInt(value);
	}

	public double getDoubleValue() {
		return Double.parseDouble(value);
	}

	public String getStringValue() {
		return value;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((section == null) ? 0 : section.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Parameter other = (Parameter) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (section == null) {
			if (other.section != null)
				return false;
		} else if (!section.equals(other.section))
			return false;
		return true;
	}

}
